<?php

namespace Common\Model;

use Think\Model;

/**
 * 用户模型
 * Class UserModel
 * @package Common\Model
 */
class UserModel extends Model {

    const FIELD = 'id,nick,email,avatar,desc,gx,sign,signs,create,msg,visit';
    const USER_KEY = 'user:';   // 用户信息缓存键名

    /**
     * 添加用户信息到数据库
     * @param $data
     * @return mixed
     */
    public static function addData($data){
        return M('User')->add($data);
    }

    /**
     * 获取用户信息
     * @param $uid
     * @param bool $add
     * @return mixed|null
     */
    public static function getUserById($uid, $add = true){
        if (!$uid) return null;
        $key = self::USER_KEY.$uid;
        $result = S($key);
        if (!$result && $result = M('User')->field(self::FIELD)->find($uid)) {
            S($key, $result);
        }
        if ($result && $add && $addData = UserCountModel::get($uid)) {
            foreach ($addData as $k => $v) {
                $result[$k] += $v;
            }
        }
        return $result;
    }

    /**
     * 更新用户信息
     * @param $uid
     * @param $data
     * @return bool
     */
    public static function update($uid, $data){
        if($result = M('User')->where(['id' => $uid])->save($data)){
            self::clearCache($uid);
        }
        return $result;
    }

    /**
     * 根据QQ openID检测用户是否存在
     * @param $openId
     * @return mixed
     */
    public static function checkUserByOpenId($openId){
        return M('User')->where(['openid' => $openId])->getField('id');
    }

    /**
     * 用户签到：连续签到$signs次增加$addGx贡献度
     * @param $uid int 用户ID
     * @param $signs int 用户连续签到次数
     * @param $addGx int 签到新增贡献度
     * @return bool
     */
    public static function sign($uid, $signs, $addGx){
        $data = [
            'sign' => time(),
            'signs' => $signs,
            'gx' => array('exp', 'gx + '.$addGx)
        ];
        if ($result = self::update($uid, $data)) {
            LogModel::userSign($uid, $signs, $addGx);
        }
        return $result;
    }

    /**
     * 修改用户的贡献值
     * @param $uid
     * @param $gx
     * @param bool $add 为true时表示增加，为false时表示减少
     * @return bool
     */
    public static function updateGx($uid, $gx = 1, $add = true){
        if($add){
            $result = M('User')->where(['id' => $uid])->setInc('gx', $gx);
        }else{
            $result = M('User')->where(['id' => $uid])->setDec('gx', $gx);
        }
        if($result){
            self::clearCache($uid);
        }
        return $result;
    }

    /**
     * 用户向另一用户捐赠
     * @param $uid
     * @param $objectId
     * @param $gx
     * @return bool
     */
    public static function donationUser($uid, $objectId, $gx)
    {
        $model = M();
        $model->startTrans();
        // 先减少当前用户的贡献度
        if(!UserModel::updateGx($uid, $gx, false)){
            $model->rollback();
            return false;
        }
        // 再更新另一个用户的贡献度
        if(!UserModel::updateGx($objectId, $gx)){
            $model->rollback();
            return false;
        }
        if (!LogModel::userDonateUser($uid, $gx, $objectId)) {
            $model->rollback();
            return false;
        }
        $model->commit();
        return true;
    }

    /**
     * 获取用户对应的昵称
     * @param $uid
     * @return mixed
     */
    public static function getUserName($uid)
    {
        if ($user = self::getUserById($uid, false)) {
            return $user['nick'];
        }
        return '';
    }

    public static function clearCache($uid){
        $key = self::USER_KEY.$uid;
        S($key, null);
    }

}